package com.halixun.d2app.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.halixun.d2app.app.MyApplication;
import com.halixun.d2app.base.BasePrintInfo;
import com.halixun.d2app.bean.print.BlueToothPrintInfo;
import com.halixun.d2app.bean.print.NetPrintInfo;
import com.halixun.d2app.bean.print.UsbPrintInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @ProjectName: D2App
 * @Package: com.halixun.d2app.utils
 * @ClassName: PrintReceiptsUtil
 * @Description: java类作用描述
 * @Author: hdx
 * @CreateDate: 2019/12/3 9:30
 * @UpdateUser: hdx
 * @UpdateDate: 2019/12/3 9:30
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class PrintReceiptsUtil {

    private static final String print_receiptes_share = "print_receiptes_share";

    private static final String key_print_receiptes = "key_print_receiptes";

    private static final String key_reset_time = "key_reset_time";

    private static final String key_reset_date = "key_reset_date";

    private static final String key_reset_state = "key_reset_state";

    private static final String key_is_fraction_to_print = "key_is_fraction_to_print";

    private static final String key_fraction_to_print_interval_time = "key_fraction_to_print_interval_time";


    private static final String key_net_print_ip = "key_net_print_ip";

    private static final String key_net_print_port = "key_net_print_port";

    private static final String key_blue_booth_print = "key_blue_booth_print";

    private static final String key_net_print = "key_net_print";

    private static final String key_usb_print = "key_usb_print";




    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);

    private static String defaultTime = "00:00";

    private static SharedPreferences getPrintReceiptsManager() {
        return MyApplication.getMyContext().getSharedPreferences(print_receiptes_share, Context.MODE_PRIVATE);
    }

    //////////////////////////////////////////////////重置单号控制//////////////////////////////////////////////////////////


    /**
     * 设置重置事件
     *
     * @param time
     * @return
     */
    public static String setResetTime(long time) {
        String resetTime = timeFormat.format(new Date(time * 1000));
        getPrintReceiptsManager().edit().putString(key_reset_time, resetTime).commit();
        if (!compareTime(resetTime)) {
            setResetState(false);
        }
        resetTime();
        return resetTime;
    }

    /**
     * 设置重置事件
     *
     * @param time
     * @return
     */
    public static String setResetTime(Date time) {
        String resetTime = timeFormat.format(time);
        getPrintReceiptsManager().edit().putString(key_reset_time, resetTime).commit();
        setResetState(false);
        if (!compareTime(resetTime)) {
            setResetState(false);
        }
        resetTime();
        return resetTime;
    }

    /**
     * 设置重置事件
     *
     * @param time
     * @return
     */
    public static String setResetTime(String time) {
        getPrintReceiptsManager().edit().putString(key_reset_time, time).commit();
        if (!compareTime(time)) {
            setResetState(false);
        }
        resetTime();
        return time;
    }


    /**
     * 重置定时
     */
    public static void resetTime() {
        if (isResetDate()) {
            if (!getResetState()) {
                if (compareTime(getResetTime())) {
                    RunningWaterUtil.resetRunningWate();
                    setResetState(true);
                }
            }
        } else {
            if (compareTime(getResetTime())) {
                RunningWaterUtil.resetRunningWate();
                setResetDate();
                setResetState(true);
            } else {
                setResetDate();
                setResetState(false);
            }
        }
    }

    /**
     * 获取记录的重置时间
     *
     * @return
     */
    public static String getResetTime() {
        return getPrintReceiptsManager().getString(key_reset_time, defaultTime);
    }

    /**
     * 设置重置日期
     */
    private static void setResetDate() {
        getPrintReceiptsManager().edit().putString(key_reset_date, dateFormat.format(new Date())).commit();
    }

    /**
     * 获取最近重置日期
     *
     * @return
     */
    private static String getResetDate() {
        return getPrintReceiptsManager().getString(key_reset_date, "");
    }

    /**
     * 是否当天
     *
     * @return
     */
    private static boolean isResetDate() {
        return dateFormat.format(new Date()).equals(getResetDate());
    }

    /**
     * 设置重置状态
     *
     * @param isReset
     */
    private static void setResetState(boolean isReset) {
        getPrintReceiptsManager().edit().putBoolean(key_reset_state, isReset).commit();
    }

    /**
     * 获取重置状态
     *
     * @return
     */
    private static boolean getResetState() {
        return getPrintReceiptsManager().getBoolean(key_reset_state, false);
    }

    /**
     * 比较时间是否该重置了
     *
     * @param oldTime
     * @return
     */
    private static boolean compareTime(String oldTime) {
        String currentTime = timeFormat.format(new Date());
        String[] currentList = currentTime.split(":");
        String[] oldList = oldTime.split(":");
        Integer currentHour = Integer.valueOf(currentList[0]);
        Integer oldHour = Integer.valueOf(oldList[0]);
        Integer currentMin = Integer.valueOf(currentList[1]);
        Integer oldMin = Integer.valueOf(oldList[1]);
        if (currentHour > oldHour) {
            return true;
        } else if (currentHour == oldHour) {
            if (currentMin >= oldMin) {
                return true;
            } else return false;
        }
        return false;
    }

    //////////////////////////////////////////是否打印小票///////////////////////////////////////////////////


    /**
     * 设置是否打印小票
     *
     * @param isPrint
     */
    public static void setPrintReceiptsState(boolean isPrint) {
        getPrintReceiptsManager().edit().putBoolean(key_print_receiptes, isPrint).commit();
    }

    /**
     * 获取是否打印小票状态
     *
     * @return
     */
    public static boolean getPrintReceiptsState() {
        return getPrintReceiptsManager().getBoolean(key_print_receiptes, true);
    }

    /////////////////////////////////////////是否分式打印，false时和式打印//////////////////////////////////////////////////

    /**
     * 是否分式打印
     *
     * @param isFractionToPrintState
     */
    public static void setFractionToPrintState(boolean isFractionToPrintState) {
        getPrintReceiptsManager().edit().putBoolean(key_is_fraction_to_print, isFractionToPrintState).commit();
    }

    /**
     * 获取分式打印状态
     *
     * @return
     */
    public static boolean getFractionToPrintState() {
        return getPrintReceiptsManager().getBoolean(key_is_fraction_to_print, false);
    }

/////////////////////////////////////////////////分式打印间隔时间设置与获取//////////////////////////////////////////////////////////

    /**
     * 获取分式打印间隔时间
     *
     * @return
     */
    public static long getFractionToPrintIntervalTime() {
        return getPrintReceiptsManager().getLong(key_fraction_to_print_interval_time, 0);
    }

    /**
     * 获取分时打印间隔时间
     *
     * @param time
     */
    public static void setFractionToPrintIntervalTime(long time) {
        getPrintReceiptsManager().edit().putLong(key_fraction_to_print_interval_time, time).commit();
    }

////////////////////////////////////////////////////网络打印机ip与port的获取与设置/////////////////////////////////////////////////////////////


    /**
     * 设置蓝牙打印数据
     * @param info
     */
    public static void setBlueBoothPrint(BlueToothPrintInfo info){
        if (info==null)return;
        List<BlueToothPrintInfo> list = getBlueBoothPrint();
        if (list==null){
            list = new ArrayList<>();
            list.add(info);
        } else {
            boolean isHas = false;
            int index = -1;
            for (BlueToothPrintInfo printInfo : list){
                if(printInfo.ip.equals(info.ip)&&printInfo.port==info.port){
                    isHas = true;
                    index = list.indexOf(printInfo);
                }
            }
            if (!isHas){
                list.add(info);
            } else {
                if (index!=-1){
                    setPrintInfoData(list.get(index),info);
                } else {
                    list.add(info);
                }
            }
        }
        Log.e("setNetPrint",new Gson().toJson(list));
        getPrintReceiptsManager().edit().putString(key_blue_booth_print,new Gson().toJson(list)).commit();
    }

    /**
     * 获取蓝牙打印信息
     * @return
     */
    public static List<BlueToothPrintInfo> getBlueBoothPrint(){
        String json = getPrintReceiptsManager().getString(key_blue_booth_print, "");
        if (!json.equals("")){
            List<BlueToothPrintInfo> list = new Gson().fromJson(json,new TypeToken<List<BlueToothPrintInfo>>(){}.getType());
            return list;
        } else return null;
    }


    /**
     * 设置网路打印数据
     * @param info
     */
    public static void setNetPrint(NetPrintInfo info){
        if (info==null)return;
        List<NetPrintInfo> list = getNetPrint();
        if (list==null){
            list = new ArrayList<>();
            list.add(info);
        } else {
            boolean isHas = false;
            int index = -1;
            for (NetPrintInfo printInfo : list){
                if(printInfo.ip.equals(info.ip)&&printInfo.port==info.port){
                    isHas = true;
                    index = list.indexOf(printInfo);
                }
            }
            if (!isHas){
                list.add(info);
            } else {
                if (index!=-1){
                    setPrintInfoData(list.get(index),info);
                } else {
                    list.add(info);
                }
            }
        }
        Log.e("setNetPrint",new Gson().toJson(list));
        getPrintReceiptsManager().edit().putString(key_net_print,new Gson().toJson(list)).commit();
    }


    /**
     * 移除网络打印数据
     * @param info
     */
    public static void removeNetPrint(NetPrintInfo info){
        if (info==null)return;
        List<NetPrintInfo> list = getNetPrint();
        if (list==null){
            list = new ArrayList<>();
        } else {
            boolean isHas = false;
            int index = -1;
            for (NetPrintInfo printInfo : list){
                if(printInfo.ip.equals(info.ip)&&printInfo.port==info.port){
                    isHas = true;
                    index = list.indexOf(printInfo);
                }
            }
            if (isHas&&index!=-1){
                list.remove(index);
            }
        }
        Log.e("setNetPrint",new Gson().toJson(list));
        getPrintReceiptsManager().edit().putString(key_net_print,new Gson().toJson(list)).commit();
    }



    /**
     * 获取网络打印数据
     * @return
     */
    public static List<NetPrintInfo> getNetPrint(){
        String json = getPrintReceiptsManager().getString(key_net_print,"");
        if (!json.equals("")){
            List<NetPrintInfo> list = new Gson().fromJson(json,new TypeToken<List<NetPrintInfo>>(){}.getType());
            Log.e("getNetPrint",new Gson().toJson(list));
            return list;
        } else return null;
    }

    /**
     * 设置usb打印数据
     * @param info
     */
    public static void setUsbPrint(UsbPrintInfo info , boolean isCover){
        if (info==null)return;
        List<UsbPrintInfo> list = getUsbPrint();
        Log.e("setUsbPrint",new Gson().toJson(list));
        if (list==null){
            list = new ArrayList<>();
            list.add(info);
        } else {
            boolean isHas = false;
            int index = -1;
            for (UsbPrintInfo printInfo : list){
                if (printInfo.name.equals(info.name)&&printInfo.id.equals(info.id)){
                    isHas = true;
                    index = list.indexOf(printInfo);
                }
            }
            Log.e("setUsbPrint",""+isHas);
            Log.e("setUsbPrint",""+index);
            if (!isHas){
                list.add(info);
            } else {
                if (index!=-1){
                    Log.e("setUsbPrint1",new Gson().toJson(list));
                    if (isCover){
                        setPrintInfoData(list.get(index),info);
                    }
                    Log.e("setUsbPrint2",new Gson().toJson(list));
                } else {
                    list.add(info);
                }
            }
        }
        Log.e("setUsbPrint",new Gson().toJson(list));
        getPrintReceiptsManager().edit().putString(key_usb_print,new Gson().toJson(list)).commit();
    }

    /**
     * 移除usb打印数据
     * @param info
     */
    public static void removeUsbPrint(UsbPrintInfo info){
        if (info==null)return;
        List<UsbPrintInfo> list = getUsbPrint();
        if (list==null){
            list = new ArrayList<>();
            list.add(info);
        } else {
            boolean isHas = false;
            int index = -1;
            for (UsbPrintInfo printInfo : list){
                if (printInfo.name.equals(info.name)||printInfo.id.equals(info.id)){
                    isHas = true;
                    index = list.indexOf(printInfo);
                }
            }
            if (isHas&&index!=-1){
                list.remove(index);
            }
        }
        getPrintReceiptsManager().edit().putString(key_usb_print,new Gson().toJson(list)).commit();
    }


    /**
     * 获取usb打印数据
     * @return
     */
    public static List<UsbPrintInfo> getUsbPrint(){
        String json = getPrintReceiptsManager().getString(key_usb_print,"");
        if (!json.equals("")){
            List<UsbPrintInfo> list = new Gson().fromJson(json,new TypeToken<List<UsbPrintInfo>>(){}.getType());
            Log.e("getUsbPrint",new Gson().toJson(list));
            return list;
        } else return null;
    }


    public static void setPrintInfoData(BasePrintInfo source , BasePrintInfo info){
        source.isPrint = info.isPrint;
        source.fractionToPrintIntervalTime = info.fractionToPrintIntervalTime;
        source.isFractionToPrint = info.isFractionToPrint;
        source.printConditions = info.printConditions;
        source.mFoodCategoryInfos = info.mFoodCategoryInfos;
    }

}
